package slice

import "fmt"

func rangArr() {
	pow := make([]int, 10)
	for i := range pow {
		pow[i] = 1 << uint(i) // == 2**i
	}
	for i := range pow {
		fmt.Printf("%d\n", i)
	}
}

func printSlice(s string, x []int) {
	fmt.Printf("%s len=%d cap=%d %v\n",
		s, len(x), cap(x), x)
}

func sliceArr() {
	a := make([]int, 5)
	printSlice("a", a)

	b := make([]int, 0, 5)
	printSlice("b", b)

	c := b[:2]
	printSlice("c", c)

	d := c[2:5]
	printSlice("d", d)
}

func printSliceSummary(s []int) {
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}

func sliceAppend() {
	var s []int
	printSliceSummary(s)

	// 可在空切片上追加
	s = append(s, 0)
	printSliceSummary(s)

	// 这个切片会按需增长
	s = append(s, 1)
	printSliceSummary(s)

	// 可以一次性添加多个元素
	s = append(s, 2, 3, 4)
	printSliceSummary(s)
}
